package cn.ante.ymcc.mq.comsumer;

import cn.ante.ymcc.dto.Order2PayDto;
import cn.ante.ymcc.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
* @Description: 支付服务消费订单服务  消费只能成功消费一次，
 * 如果消息被别人消费了，会显示消息已签收，不会再推送给另一个消费者，怎么标记已签收：只要方法不报错就自动签收了
* @Author: Nai
* @Version: 1.0
* @Date:  2022/8/20 22:30
*/
@Component
@RocketMQMessageListener(topic = "topic-order",
        consumerGroup = "service-pay-consumer",
        messageModel = MessageModel.CLUSTERING,
        selectorExpression = "tags-payorder"
)
public class Order2PayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt message) {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        Order2PayDto order2PayDto = JSONObject.parseObject(msg, Order2PayDto.class);
        payOrderService.savePayOrder(order2PayDto);


    }
}
